package com.kc.business.service.impl;

import com.kc.business.domain.KcHistoricalFigure;
import com.kc.business.mapper.KcHistoricalFigureMapper;
import com.kc.business.service.KcHistoricalFigureService;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * FileName: KcHistoricalFigureServiceImpl
 * Author:   TP
 * Date:     2020-11-09 14:59
 * Description:
 */
@Service
public class KcHistoricalFigureServiceImpl implements KcHistoricalFigureService {

    @Autowired
    private KcHistoricalFigureMapper historicalFigureMapper;

    @Override
    public List<KcHistoricalFigure> getList(KcHistoricalFigure historicalFigure) {
        return historicalFigureMapper.getList(historicalFigure);
    }

    @Override
    public KcHistoricalFigure getById(Integer id) {
        if (id != null && id > 0) {
            return historicalFigureMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public KcHistoricalFigure getByName(String name) {
        if (StringUtils.isNotEmpty(name)) {
            KcHistoricalFigure figure = new KcHistoricalFigure();
            figure.setHistoricalFigureName(name);
            return historicalFigureMapper.selectOne(figure);
        }
        return null;
    }

    @Override
    public AjaxResult add(KcHistoricalFigure figure) {
        if (figure != null) {
            // 查重
            KcHistoricalFigure exist = getByName(figure.getHistoricalFigureName());
            if (exist != null) {
                return AjaxResult.error(String.format("已存在名字为[%s]的历史人物信息", exist.getHistoricalFigureName()));
            }
            if (historicalFigureMapper.insert(figure) > 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，数据持久化失败");
        }
        return AjaxResult.error("操作失败，历史人物信息不能为空");
    }

    @Override
    public AjaxResult update(KcHistoricalFigure figure) {
        if (figure.getId() != null && figure.getId() > 0) {
            // 存在性校验
            KcHistoricalFigure existFigure = historicalFigureMapper.selectByPrimaryKey(figure.getId());
            if (existFigure == null) {
                return AjaxResult.error("操作失败，未查询到此历史人物信息，请刷新重试");
            }
            // 重复性校验
            KcHistoricalFigure originFigure = getByName(figure.getHistoricalFigureName());
            if (originFigure != null && originFigure.getHistoricalFigureName().equals(figure.getHistoricalFigureName())
                    && !figure.getId().equals(originFigure.getId())) {
                return AjaxResult.error("操作失败，已存在同名历史人物，请修改名称后重试");
            }
            if (historicalFigureMapper.updateByPrimaryKeySelective(figure) > 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，数据库持久化失败");
        }
        return AjaxResult.error("操作失败，请指定要更新的历史人物信息");
    }

    @Override
    public int deleteById(Integer id) {
        if (id != null && id > 0) {
            return historicalFigureMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }
}
